package com.hhs.storage.controller;


import com.hhs.storage.bean.*;
import com.hhs.storage.service.PersonalPlanService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hhs
 * @since 2023-01-13
 */
@RestController
@RequestMapping("/personalPlan")
public class PersonalPlanController {
    @Resource
    private PersonalPlanService personalPlanService;

    @RequestMapping(value = "table")
    public Map<String, Object> getAll(PersonalPlan personalPlan, InputParam inputParam) {
        return personalPlanService.getAll(personalPlan, inputParam);
    }

    /**
     * 查询指定时间访问内的空闲任务
     *
     * @param personalPlan
     * @param inputParam
     * @return
     */
    @RequestMapping(value = "freeTable")
    public Map<String, Object> freeTable(PersonalPlan personalPlan, InputParam inputParam) {
        return personalPlanService.freeTable(personalPlan, inputParam);
    }

    /**
     * 查询部门及子部门所有用户每人每日的所有任务
     *
     * @param departIds
     * @param workDate
     * @param inputParam
     * @return
     */
    @PostMapping(value = "getDepartmentDailyTable")
    public Map<String, Object> getDepartmentDailyTable(@RequestParam String departIds, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate workDate, InputParam inputParam) {
        return personalPlanService.getDepartmentDailyTable(departIds, workDate, inputParam);
    }

    /**
     * 查询关注的人每日任务
     *
     * @param workDate
     * @param inputParam
     * @return
     */
    @PostMapping(value = "getFollowUserDailyTable")
    public Map<String, Object> getFollowUserDailyTable(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate workDate, String groupId, InputParam inputParam) {
        return personalPlanService.getFollowUserDailyTable(workDate, groupId, inputParam);
    }

    /**
     * 查询项目所有用户每人每日的所有任务
     *
     * @param projectId  项目id
     * @param workDate
     * @param inputParam
     * @return
     */
    @PostMapping(value = "getProjectDailyTable")
    public Map<String, Object> getProjectDailyTable(@RequestParam String projectId, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate workDate, InputParam inputParam) {
        return personalPlanService.getProjectDailyTable(projectId, workDate, inputParam);
    }

    /**
     * 查询指定用户一周内任务
     *
     * @param userId   用户id
     * @param workDate 某周日期
     * @return
     */
    @PostMapping(value = "getPersonalPlanByWeek")
    public Map<String, Object> getPersonalPlanByWeek(String userId, String projectId, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate workDate, Boolean secondWeek) {
        return personalPlanService.getPersonalPlanByWeek(userId, projectId, workDate, secondWeek);
    }

    /**
     * 根据名字得到日期值
     *
     * @param inputParam
     * @return
     */
    @PostMapping(value = "changeWeek")
    public ResponseBean<String> changeWeek(InputParam inputParam) {
        return personalPlanService.changeWeek(inputParam);
    }

    /**
     * 查询部门及子部门每日最新完成任务
     *
     * @param departIds
     * @param workDate
     * @param inputParam
     * @return
     */
    @PostMapping(value = "getDepartmentDailyPlan")
    public Map<String, Object> getDepartmentDailyPlan(@RequestParam String departIds, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate workDate, InputParam inputParam) {
        return personalPlanService.getDepartmentDailyPlan(departIds, workDate, inputParam);
    }

    @PostMapping(value = "edit")
    public Map<String, Object> addOrUpdate(PersonalPlan personalPlan) {
        return personalPlanService.addOrUpdate(personalPlan);
    }

    @PostMapping(value = "multiEdit")
    public Map<String, Object> multiEdit(PersonalPlan personalPlan, String planIds) {
        return personalPlanService.editByPlanIds(personalPlan, planIds);
    }

    @PostMapping(value = "dele")
    public Map<String, Object> dele(String id) {
        try {
            return personalPlanService.dele(id, false);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseInfo.error("存在关联业务，无法删除");
        }

    }

    @PostMapping(value = "stateChange")
    public Map<String, Object> stateChange(String id, Integer state) {
        return personalPlanService.stateChange(id, state);

    }

    /**
     * 当天已使用时间段
     *
     * @param userId   指定用户
     * @param workDate 指定时间
     * @return
     */
    @PostMapping(value = "getUsedTime")
    public Map<String, Object> getUsedTime(String userId, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate workDate) {
        return personalPlanService.getUsedTime(userId, workDate);
    }

    /**
     * 删除指定日期指定时间段任务
     *
     * @param userId
     * @param workDate
     * @param startTime
     * @param endTime
     * @return
     */
    @PostMapping(value = "deleAll")
    public Map<String, Object> deleAll(String userId, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate workDate,
                                       @RequestParam @DateTimeFormat(pattern = "HH:mm") LocalTime startTime,
                                       @RequestParam @DateTimeFormat(pattern = "HH:mm") LocalTime endTime) {
        return personalPlanService.deleAll(userId, workDate, startTime, endTime);
    }

    /**
     * 删除指定日期任务
     *
     * @param userId
     * @param workDate
     * @return
     */
    @PostMapping(value = "deleAllDay")
    public Map<String, Object> deleAllDay(String userId, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate workDate) {
        return personalPlanService.deleAllDay(userId, workDate);
    }

    /**
     * 完成指定日期指定时间段任务
     *
     * @param userId
     * @param workDate
     * @param startTime
     * @param endTime
     * @return
     */
    @PostMapping(value = "completeAll")
    public Map<String, Object> completeAll(String userId, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate workDate,
                                           @RequestParam @DateTimeFormat(pattern = "HH:mm") LocalTime startTime,
                                           @RequestParam @DateTimeFormat(pattern = "HH:mm") LocalTime endTime) {
        return personalPlanService.completeAll(userId, workDate, startTime, endTime);
    }

    @PostMapping(value = "completeByPlanIds")
    public Map<String, Object> completeByPlanIds(String planIds) {
        return personalPlanService.completeByPlanIds(planIds);
    }

    @PostMapping(value = "uncompleteByPlanIds")
    public Map<String, Object> uncompleteByPlanIds(String planIds) {
        return personalPlanService.uncompleteByPlanIds(planIds);
    }

    @PostMapping(value = "deleByPlanIds")
    public Map<String, Object> deleByPlanIds(String planIds) {
        return personalPlanService.deleByPlanIds(planIds);
    }

    /**
     * 获取个人计划统计数据
     *
     * @param inputParam name 用户姓名
     * @param inputParam id 部门id
     * @param startDate  开始日期
     * @param endDate    结束日期
     * @return
     */
    @PostMapping(value = "statisticalByUser")
    public Map<String, Object> statisticalByUser(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate, InputParam inputParam) {
        return personalPlanService.statisticalByUser(startDate, endDate, inputParam);

    }

    /**
     * 获取关注的人的个人计划统计数据
     *
     * @param inputParam name 用户姓名
     * @param startDate  开始日期
     * @param endDate    结束日期
     * @return
     */
    @PostMapping(value = "statisticalByFollowUser")
    public Map<String, Object> statisticalByFollowUser(
            String groupId,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate, InputParam inputParam) {
        return personalPlanService.statisticalByFollowUser(groupId, startDate, endDate, inputParam);

    }

    /**
     * 获取站点统计数据
     *
     * @param inputParam name 用户姓名
     * @param inputParam id 部门id
     * @param startDate  开始日期
     * @param endDate    结束日期
     * @return
     */
    @PostMapping(value = "statisticalSiteByUser")
    public Map<String, Object> statisticalSiteByUser(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate, InputParam inputParam) {
        return personalPlanService.statisticalSiteByUser(startDate, endDate, inputParam);

    }

    /**
     * 获取当天未完成任务
     *
     * @return
     */
    @RequestMapping(value = "getTodayUndoPlan")
    public Map<String, Object> getTodayUndoPlan() {
        return personalPlanService.getTodayUndoPlan();

    }

    /**
     * 根据项目id获取该员工所有的项目计划
     * curUserId  员工id
     * project   项目id
     *
     * @return
     */
    @RequestMapping(value = "getPlanByProId")
    public Map<String, Object> getPlanByProId(InputParam inputParam) {
        return personalPlanService.getPlanByProId(inputParam);

    }
}

